package cn.wzvtc.demo.common.dto;
import cn.wzvtc.demo.common.constant.CommonStatus;
import cn.wzvtc.demo.common.exception.CommonException;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应数据
 * @author Rex
 * @create 2021-06-01 15:11
 */
@Data
public class Result<T> implements Serializable {
  private static final long serialVersionUID = 1L;

  /**
   * 响应码
   */
  private Integer code;

  /**
   * 响应信息
   */
  private String msg;

  /**
   * 响应数据
   */
  private T data;

  public Result() {
  }

  public Result(Integer code, String msg) {
    this.code = code;
    this.msg = msg;
  }

  public Result(Integer code, String msg, T data) {
    this(code, msg);
    this.data = data;
  }

  public Result(CommonStatus commonStatus) {
    this(commonStatus.getCode(), commonStatus.getMsg());
  }

  public Result(CommonStatus commonStatus, T data) {
    this(commonStatus);
    this.data = data;
  }

  public static <T>Result<T> success(){
    return new Result<T>(CommonStatus.SUCCESS);
  }

  public static <T>Result<T> success(T data){
    return new Result<T>(CommonStatus.SUCCESS, data);
  }

  public static <T>Result<T> success(String msg){
    return new Result<T>(CommonStatus.SUCCESS.getCode(), msg);
  }

  public static <T>Result<T> error(CommonStatus commonStatus){
    return new Result<>(commonStatus);
  }

  public static <T>Result<T> error(CommonException commonException){
    return new Result<>(commonException.getCode(), commonException.getMsg());
  }
  public static <T>Result<T> error(Integer code, String msg){
    return new Result<>(code, msg);
  }
}
